import Common from '../common'
var Js = Common('adminLists')
export default {
	name: 'adminLists',
	components: {},
	mixins: [Js],
	data() {
		return {
			mainData: [],
			self: this,

			table_arguments: {
				height: '70%',
				row_key: 'id',
				tree_props: '',
				default_expand_all: false,
				expand: false,
				selection: true,
				cell_style: {}
			},
			fields: [{
					key: 'id',
					label: '管理员ID',
					application: [],
					componentName: 'sls-input',
					listType: ''
				},
				{
					key: 'login_name',
					label: '管理员登录名',
					application: ['编辑账号', '添加账号'],
					componentName: 'sls-input',
					listType: 'normal',
					placeholder: '请输入管理员登录名',
					header_search: {
						componentName: 'sls-input',
						style: 'width:160px;margin-right:2px;',
						placeholder: '请输入管理员登录名',
						changeFunc: function(e, self) {
							if (e.target._value) {
								self.searchItem.login_name = ['LIKE', ['%' + e.target._value + '%']];
							} else {
								delete self.searchItem.login_name;
							};

						},
					}
				},
				{
					key: 'mainImg',
					label: '管理员头像',
					application: ['编辑账号', '添加账号'],
					customSlot: 'mainImg',
					componentName: 'upload',
					listType: 'normal',
					limit: 10,
				},
				{
					key: 'password',
					label: '管理员密码',
					application: ['编辑账号', '添加账号'],
					componentName: 'sls-input',
					dialog: {
						type: 'password'
					}
				},
				{
					key: 'create_time',
					label: '创建时间',
					listType: 'normal',
					placeholder: '请选择创建时间',
					header_search: {
						componentName: 'sls-date-time-range',
						style: 'width:160px;margin-right:2px;',
						start_placeholder: '创建开始时间',
						end_placeholder: '创建结束时间',
						changeFunc: function(value, self) {
							if (!value) {
								delete self.searchItem.create_time;
							} else {
								self.searchItem.create_time = ['between', value = value.map(function(e) {
									return e / 1000;
								})]
							};
							
						},
					},
				},
				{
					label: '操作',
					listType: 'deal',
					width: 200
				},
			],
			// 按钮配置
			btn_info: [

				{
					type: 'info',
					icon: 'edit',
					size: 'mini',
					position: 'list',
					text: function(data) {
						if (data) {
							return JSON.stringify(data.info) != '[]' ? '编辑信息' : '添加信息'
						} else {
							return ['编辑信息', '添加信息'];
						}
					},
					func: {
						apiName: function(self) {
							return JSON.stringify(self.formData.info) != "[]" ? "api_userInfoUpdate" : "api_userInfoAdd"
						},

						formData: function(self) {

							var data = {
								name: (self.formData.info && self.formData.info.name) ? self.formData.info.name : '',
								phone: (self.formData.info && self.formData.info.phone) ? self.formData.info.phone : '',
							};
							return data
						},

						postData: function(self) {
							if (self.btnName == '编辑信息') {
								var postData = {
									searchItem: {
										id: self.originFormData.info.id,
										user_no: self.originFormData.info.user_no
									},
									data: self.submitData
								}
							} else if (self.btnName == '添加信息') {
								var postData = {
									data: self.submitData
								};
							};
							postData.data.user_no = self.originFormData.user_no;

							return postData;
						}
					},
				},
				{
					type: 'info',
					icon: 'edit',
					size: 'mini',
					position: 'list',
					text: function(data) {
						return '编辑账号'
					},
					func: {
						apiName: function(self) {
							return "api_userUpdate"
						},
						formData: function(self) {
							return self.formData
						},
						postData: function(self) {
							var postData = {
								searchItem: {
									id: self.formData.id,
									user_no: self.formData.user_no,
								},
								data: self.submitData
							}
							return postData;
						}
					},
				},
				{
					type: 'info',
					icon: 'edit',
					size: 'mini',
					position: 'list',
					text: function(data) {
						return '管理权限'
					},
					isHide: function(self) {
						if (self.formData.user_no == self.$store.getters.getUserinfo.user_no && self.$store.getters.getUserinfo.primary_scope <
							60) {
							return true;
						} else {
							return false;
						};
					},
					funcType: 'func',
					func: {
						func: function(self) {
							console.log('self.originFormData', self.originFormData);
							self.$router.push({
								path: '/home/access',
								name: '权限管理',
								params: {
									defaultChecked: self.originFormData.auth,
									role: self.originFormData.role,
									user_no: self.originFormData.user_no,
                  behavior:0,
									primary_scope: self.originFormData.primary_scope,
									path: '/home/adminLists'
								}
							});
						},
						postData: function(self) {
							var postData = {
								searchItem: {
									id: self.formData.id,
								},
								data: data
							};
							return postData;
						},
					},
				},
				{
					type: 'danger',
					icon: 'delete',
					size: 'mini',
					funcType: 'submit',
					position: 'header',
					text: function(data) {
						return '删除选中'
					},
					func: {

						apiName: function(self) {
							return "api_userUpdate"
						},

						postData: function(self) {
							var deleteArray = [];
							for (var i = 0; i < self.selectionArray.length; i++) {
								deleteArray.push(self.selectionArray[i].id);
							};
							var postData = {
								searchItem: {
									id: ['in', deleteArray],
								},
								data: {
									status: -1
								}
							};
							return postData;
						}

					},
				},
				{
					type: 'info',
					icon: 'edit',
					size: 'mini',
					position: 'header',
					text: function(data) {
						return '添加账号'
					},
					func: {
						apiName: function(self) {
							return "api_userAdd"
						},
						formData: function(self) {
							var data = {
								login_name: '',
								password: '',
							};
							return data
						},
						postData: function(self) {
							self.formData.user_type = 2;
							var postData = {
								data: self.submitData
							};
							return postData;
						}
					},
				},
			],
			paginate: {
				count: 0,
				currentPage: 1,
				pagesize: 10,
				is_page: true,
				page_sizes: [10, 30, 60, 90],
				layout: 'total, sizes, prev, pager, next, jumper',
			},
			searchItem: {
				user_type: 2
			},
			startSearchItem: {
				user_type: 2
			},
			optionData: {
				labelOptions: [],
				roleOptions: [],
				statusOptions: [{
					text: '启用',
					value: 1
				}, {
					text: '禁用',
					value: -1
				}],
			},
			otherData: {},
			UserInfo: {
				tableName: 'UserInfo',
				searchItem: {},
				fixSearchItem: {
					status: 1
				},
				key: 'user_no',
				middleKey: 'user_no',
				condition: 'in',
			},
			dialog: {
				formLabelWidth: 'auto',
				dialogFormVisible: false,
				title: '用户'
			},
			getBefore: {},
			formLabelWidth: 'auto',
			btnName: '',
			formData: {},
			btnNow: {},
			submitData: {},
			originFormData: {},
			selectionArray: []

		}

	},

	mounted() {
		this.init()
	},
	computed: {
		token: function() {
			return this.$store.getters.getToken
		}
	},
	watch: {
		$route(to, from) {
			// console.log(to)
			// this.init()
		},
		token() {

		}
	},
	methods: {

		/**
		 * 初始化
		 */
		init() {
			this.initMainData()
			this.initRoleData()
		},

		clearSearch() {
			const self = this;
			self.searchItem = {
				user_type: 2
			};
			self.initMainData(true);
		},


		async initRoleData() {

			const self = this;
			const postData = {};

			postData.token = self.$store.getters.getToken;
			postData.searchItem = {
				status: 1
			};
			var res = await self.$$api_roleGet({
				data: postData
			});
			self.optionData.roleOptions = res.info.data;

		},


		/**
		 * 列表主函数
		 */
		async initMainData(isNew) {

			const self = this;
			const postData = {};
			if (isNew) {
				self.paginate.currentPage = 1;
			};
			postData.paginate = self.$$cloneForm(self.paginate);
			postData.token = self.$store.getters.getToken;
			if (self.searchItem) {
				postData.searchItem = self.$$cloneForm(self.searchItem)
			};
			if (JSON.stringify(self.getBefore) != "{}") {
				postData.getBefore = self.$$cloneForm(self.getBefore);
			};
			var res = await self.$$api_userGet({
				data: postData
			});
			self.mainData = res.info.data;
			self.paginate.count = res.info.total;

		},



	},


}
